/**
 * Created by Administrator on 16-10-21.
 */
Ext.onReady(function () {
    Ext.QuickTips.init();
    var sszmdygx_grid_row;
    var stnm_message = new Ext.data.JsonStore({
        url: '/qtIndex/stationIdAndNm/combo.action',
        fields: ['value', 'text']
    });
    stnm_message.load();
    var stnm_message_combo_add = new Ext.form.ComboBox({
        name: 'stnm',
        fieldLabel: '测站名称',
        emptyText: '请选择',
        mode: 'local',
        editable: true,
        triggerAction: 'all',
        store: stnm_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'stcd',
        allowBlank: false,
        msgTarget: "side",
        queryParam: 'userinput',
        forceSelection: true,
        listeners: {
            beforequery: function (qe) {
                var para = qe.query;
            }
        }
    });
    var stnm_message_combo_upd = new Ext.form.ComboBox({
        name: 'stnm',
        fieldLabel: '测站名称',
        emptyText: '请选择',
        mode: 'local',
        editable: true,
        triggerAction: 'all',
        store: stnm_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'stcd',
        allowBlank: false,
        msgTarget: "side",
        queryParam: 'userinput',
        forceSelection: true,
        listeners: {
            beforequery: function (qe) {
                var para = qe.query;
            }
        }
    });

    var ssxmxx_message = new Ext.data.JsonStore({
        url: '/qtIndex/ssxmxx/combo.action',
        fields: ['value', 'text']
    });
    ssxmxx_message.load();
    var ssxmxx_message_combo_add = new Ext.form.ComboBox({
        name: 'obitnm',
        fieldLabel: '施测项目',
        emptyText: '请选择',
        mode: 'local',
        editable: false,
        triggerAction: 'all',
        store: ssxmxx_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'obitcd',
        allowBlank: false,
        msgTarget: "side"
    });
    var ssxmxx_message_combo_update = new Ext.form.ComboBox({
        name: 'obitnm',
        fieldLabel: '施测项目',
        emptyText: '请选择',
        mode: 'local',
        editable: false,
        triggerAction: 'all',
        store: ssxmxx_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'obitcd',
        allowBlank: false,
        msgTarget: "side"
    });

    var mlx_message = new Ext.data.JsonStore({
        url: '/qtIndex/mlx/combo.action',
        fields: ['value', 'text']
    });
    mlx_message.load();
    var mlx_message_combo_add = new Ext.form.ComboBox({
        name: 'tynm',
        fieldLabel: '码类型',
        emptyText: '请选择',
        mode: 'local',
        editable: false,
        triggerAction: 'all',
        store: mlx_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'tycd',
        allowBlank: false,
        msgTarget: "side"
    });
    var mlx_message_combo_udp = new Ext.form.ComboBox({
        name: 'tynm',
        fieldLabel: '码类型',
        emptyText: '请选择',
        mode: 'local',
        editable: false,
        triggerAction: 'all',
        store: mlx_message,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'tycd',
        allowBlank: false,
        msgTarget: "side"
    });

    var flag_store = new Ext.data.JsonStore({
        fields: ['value', 'text'],
        data: [
            {text: "是", value: 1},
            {text: "否", value: 0}
        ]
    });

    var flag_combo = new Ext.form.ComboBox({
        name: 'flag',
        fieldLabel: '首选码',
        emptyText: '请选择',
        mode: 'local',
        editable: false,
        triggerAction: 'all',
        store: flag_store,
        width: 150,
        valueField: 'value', // 值
        displayField: 'text',
        hiddenName: 'first_flag',
        allowBlank: true,
        msgTarget: "side"
    });

    var sskzmdygx_reader = new Ext.data.JsonReader({
        totalProperty: 'totalProperty',
        root: 'root'
    }, [
        {
            name: 'stcd'
        },
        {
            name: 'tycd'
        },
        {
            name: 'obitcd'
        }, {
            name: 'sscd'
        }, {
            name: 'first_flag'
        },
        {
            name: 'frdt'
        },
        {
            name: 'nt'
        },
        {
            name: 'stnm'
        },
        {
            name: 'tynm'
        },
        {
            name: 'obitnm'
        },
        {
            name: 'dtstr'
        }
    ]);
    var sskzmdygx_store = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/qtIndex/sskzmdygx/showall.action?stcd=0101.0001'
        }),
        reader: sskzmdygx_reader
    });

    var sskzmdygx_addform = new Ext.form.FormPanel({
        labelAlign: 'right',
        frame: true,
        labelWidth: 150,
        url: '/qtIndex/sskzmdygx/add.action?stcd=0101.0001',
        bodyStyle: 'padding:30px',
        reader: sskzmdygx_reader,
        items: [
            // stnm_message_combo_add,
            mlx_message_combo_add,
            ssxmxx_message_combo_add,
            {
                xtype: 'textfield',
                fieldLabel: '站码',
                width: 150,
                name: 'sscd'
            },
            flag_combo,
            {
                xtype: 'datefield',
                fieldLabel: '启用日期',
                width: 150,
                format: 'Y-m-d H:i:s',
                name: 'frdt',
                emptyText: '请选择',
                allowBlank: true,
                msgTarget: "side"
            },
            {
                xtype: 'textarea',
                fieldLabel: '备注',
                width: 150,
                name: 'nt'
            }
        ]
    });
    var sskzmdygx_updform = new Ext.form.FormPanel({
        labelAlign: 'right',
        frame: true,
        labelWidth: 150,
        url: '/qtIndex/sskzmdygx/update.action',
        bodyStyle: 'padding:30px',
        reader: sskzmdygx_reader,
        items: [
            stnm_message_combo_upd,
            mlx_message_combo_udp,
            ssxmxx_message_combo_update,
            {
                xtype: 'textfield',
                fieldLabel: '站码',
                width: 150,
                name: 'sscd'
            },
            {
                xtype: 'datefield',
                fieldLabel: '启用日期',
                width: 150,
                format: 'Y-m-d H:i:s',
                name: 'frdt',
                emptyText: '请选择',
                allowBlank: true,
                msgTarget: "side"
            },
            {
                xtype: 'textarea',
                fieldLabel: '备注',
                width: 150,
                name: 'nt'
            }
        ]
    });

    //表格的列头
    var sszmdygx_colModel = new Ext.grid.ColumnModel([
        new Ext.grid.RowNumberer({
            header: '编号',
            width: 35
        }),
        {
            header: "码类型",
            sortable: true,
            dataIndex: 'tynm',
            width: 370
        }, {
            header: "施测项目",
            sortable: true,
            dataIndex: 'obitnm',
            width: 300
        }, {
            header: "站码",
            sortable: true,
            dataIndex: 'sscd',
            width: 300
        },{
            header: "首选项",
            sortable: true,
            dataIndex: 'first_flag',
            width: 300
        },{
            header: "启用日期",
            sortable: true,
            dataIndex: 'dtstr',
            width: 300
        },{
            header: "备注",
            sortable: true,
            dataIndex: 'nt',
            width: 300
        }
        , {
            xtype: 'actioncolumn',
            header: "操作",
            items: [
                {
                    icon: '/jsps/qt/images/delete.jpg',  // Use a URL in the icon config
                    tooltip: '删除',
                    handler: function (grid, rowIndex, colIndex) {
                        var rec = sskzmdygx_store.getAt(rowIndex);
                        Ext.MessageBox.confirm('删除', '确认删除？', function (btn) {
                            if (btn == 'yes') {
                                Ext.Ajax.request({
                                    url: '/qtIndex/sskzmdygx/delete.action',
                                    params: {
                                        stcd: rec.get('stcd'),
                                        tycd: rec.get('tycd'),
                                        obitcd:rec.get('obitcd')

                                    },
                                    success: function (response) {
                                        Ext.Msg.alert('提示',
                                            '删除成功！');
                                        sskzmdygx_store.load({
                                            params: {
                                                start : 0,
                                                limit: 30
                                            }
                                        });

                                    },
                                    failure: function (response) {
                                        Ext.Msg.alert('提示',
                                            '删除失败！');
                                    }

                                });

                            }

                        });
                    }
                }

            ]
        }

    ]);

    var add_win = new Ext.Window({
        layout: 'fit',
        title: '实时库站码对应关系添加',
        width: '500',
        height: '400',
        closeAction: 'hide',
        constrain: true,
        buttonAlign: 'center',
        resizable: false,
        modal: true,
        plain: true,
        items: [sskzmdygx_addform],
        buttons: [
            {
                text: '增加',
                handler: function () {
                    // alert(JSON.stringify(add_form.getForm().getValues()));
                    Ext.Ajax.request({
                        url: '/qtIndex/sskzmdygx/check.action',
                        params: {
                            sscd: sskzmdygx_addform.getForm().findField('sscd').getValue(),
                        },
                        success: function (response) {
                            var respText = Ext.util.JSON.decode(response.responseText);
                            if(!respText.success){
                                Ext.MessageBox.confirm('删除', '存在相同的站码,确认添加？', function (btn) {
                                    if (btn == 'yes') {
                                        sskzmdygx_addform.getForm().submit({
                                            success: function (form, action) {

                                                Ext.Msg.alert('提示',
                                                    '提交成功！');
                                                sskzmdygx_addform.getForm().reset();
                                                add_win.hide();
                                                t.ajax.reload();
                                            },
                                            failure: function () {
                                                Ext.Msg.alert('提示',
                                                    '提交失败！');
                                            }
                                        });
                                    }
                                });
                            }else {
                                sskzmdygx_addform.getForm().submit({
                                    success: function (form, action) {

                                        Ext.Msg.alert('提示',
                                            '提交成功！');
                                        sskzmdygx_addform.getForm().reset();
                                        add_win.hide();
                                        t.ajax.reload();
                                    },
                                    failure: function () {
                                        Ext.Msg.alert('提示',
                                            '提交失败！');
                                    }
                                });
                            }

                        },
                        failure: function (response) {
                        }

                    });


                }
            },
            {
                text: '取消',
                handler: function () {
                    add_win.hide();
                }
            }
        ]
    });


    var sszmdygx_grid = new Ext.grid.GridPanel({
        title: "实时站码对应关系",
        width: 1100,
        height: 670,
        renderTo: 'sszmdygx',
//        region:'center',
        cm: sszmdygx_colModel,
//        collapsed:true,
        autoExpandColumn: 2,
        stripeRows: true,
        loadMask: true,
        store: sskzmdygx_store,
        tbar: new Ext.Toolbar(['->',
            {
                icon: '/ext/add.gif',
                tooltip:'增加',
                handler: function () {
                    add_win.show();
                }
            }
        ]),
        bbar: new Ext.PagingToolbar({
            firstText: '1',
            pageSize: 30,
            store: sskzmdygx_store,
            displayInfo: true,
            displayMsg: '显示第 {0} 到 {1}条记录 ，共 {2}条',
            emptyMsg: "没有记录"
        }),
        viewConfig: {
            forceFit: true
        }

    });
    sskzmdygx_store.load({
        params: {
            start :0,
            limit:30
        }
    });

});
